﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class CalcDuctForm : System.Windows.Forms.Form
	{
		public CalcDuctForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{643BDE60-15D6-414F-868E-ABF46CAFC377}", null);
			base.Icon = Resource.YArchitechIcon;
			this.useSetting = new HVACUserSetting();
			this.cds = new CalcDuctSpeed();
			this.cds = this.useSetting.GetCalcDuctSpeed();
		}

		private void CalcDuctForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.txtFlow.Text = this.cds.DuctFlow.ToString();
			this.cmbSection.SelectedIndex = this.cds.SecType;
			this.txtWidth.Text = this.cds.DuctWidth.ToString();
			this.txtHeight.Text = this.cds.DuctHeight.ToString();
			double flow = Convert.ToDouble(this.txtFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			if (this.cmbSection.SelectedIndex == 1)
			{
				this.txtHeight.Visible = false;
				this.lblSpeed.Text = "V(m/s) = " + this.CalcRouDuctSpeed(flow, num);
				this.InitialListView(this.GetRoundSec(), this.lvWidth, "直径", -1);
				this.lvHeight.Visible = false;
				this.btnChange.Visible = false;
				this.SelectViewItem(this.lvWidth, num);
				return;
			}
			this.txtHeight.Visible = true;
			this.txtHeight.Text = this.cds.DuctHeight.ToString();
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.InitialListView(this.GetRectangeSec(), this.lvWidth, "宽(W)", num2);
			this.InitialListView(this.GetRectangeSec(), this.lvHeight, "高(H)", num);
			this.lblSpeed.Text = "V(m/s) = " + this.CalcRecDuctSpeed(flow, num, num2);
			this.lvHeight.Visible = true;
			this.btnChange.Visible = true;
			this.SelectViewItem(this.lvWidth, num);
			this.SelectViewItem(this.lvHeight, num2);
		}

		private void txtWidth_TextChanged(object sender, EventArgs e)
		{
			double flow = Convert.ToDouble(this.txtFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			if (this.cmbSection.SelectedIndex == 1)
			{
				this.lblSpeed.Text = "V(m/s) = " + this.CalcRouDuctSpeed(flow, num);
				this.SelectViewItem(this.lvWidth, num);
				return;
			}
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.InitialListView(this.GetRectangeSec(), this.lvHeight, "高(H)", num);
			this.lblSpeed.Text = "V(m/s) = " + this.CalcRecDuctSpeed(flow, num, num2);
			this.SelectViewItem(this.lvHeight, num2);
		}

		private void txtHeight_TextChanged(object sender, EventArgs e)
		{
			double flow = Convert.ToDouble(this.txtFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.InitialListView(this.GetRectangeSec(), this.lvWidth, "宽(W)", num2);
			this.lblSpeed.Text = "V(m/s) = " + this.CalcRecDuctSpeed(flow, num, num2);
			this.SelectViewItem(this.lvWidth, num);
		}

		private void ComBoxChangeBanding()
		{
			double flow = Convert.ToDouble(this.txtFlow.Text);
			int num = Convert.ToInt32(this.txtWidth.Text);
			if (this.cmbSection.SelectedIndex == 1)
			{
				this.txtHeight.Visible = false;
				this.lblSpeed.Text = "V(m/s) = " + this.CalcRouDuctSpeed(flow, num);
				this.InitialListView(this.GetRoundSec(), this.lvWidth, "直径", -1);
				this.lvHeight.Visible = false;
				this.btnChange.Visible = false;
				this.SelectViewItem(this.lvWidth, num);
				return;
			}
			this.txtHeight.Visible = true;
			this.txtHeight.Text = this.cds.DuctHeight.ToString();
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.InitialListView(this.GetRectangeSec(), this.lvWidth, "宽(W)", num2);
			this.InitialListView(this.GetRectangeSec(), this.lvHeight, "高(H)", num);
			this.lblSpeed.Text = "V(m/s) = " + this.CalcRecDuctSpeed(flow, num, num2);
			this.lvHeight.Visible = true;
			this.btnChange.Visible = true;
			this.SelectViewItem(this.lvWidth, num);
			this.SelectViewItem(this.lvHeight, num2);
		}

		private void SelectViewItem(ListView lvView, int txtValue)
		{
			System.Drawing.Color backColor = lvView.BackColor;
			System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
			for (int i = 0; i < lvView.Items.Count; i++)
			{
				ListViewItem listViewItem = lvView.Items[i];
				listViewItem.BackColor = backColor;
				if (Convert.ToInt32(listViewItem.SubItems[0].Text) == txtValue)
				{
					lvView.Items[i].Selected = true;
					lvView.Items[i].EnsureVisible();
					lvView.Items[i].Focused = true;
					lvView.Items[i].BackColor = lightSkyBlue;
				}
			}
		}

		private void InitialListView(List<int> secList, ListView listView, string colNum, int whValue)
		{
			listView.Items.Clear();
			listView.Columns.Clear();
			double flow = Convert.ToDouble(this.txtFlow.Text);
			this.InitialLVColumn(ref listView, colNum);
			if (whValue < 0)
			{
				using (List<int>.Enumerator enumerator = secList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						int diameter = enumerator.Current;
						ListViewItem listViewItem = new ListViewItem();
						listViewItem.SubItems.Clear();
						listViewItem.SubItems[0].Text = diameter.ToString();
						double num = this.CalcRouDuctSpeed(flow, diameter);
						listViewItem.SubItems.Add(num.ToString());
						listView.Items.Add(listViewItem);
					}
					return;
				}
			}
			foreach (int width in secList)
			{
				ListViewItem listViewItem2 = new ListViewItem();
				listViewItem2.SubItems.Clear();
				listViewItem2.SubItems[0].Text = width.ToString();
				double num2 = this.CalcRecDuctSpeed(flow, width, whValue);
				listViewItem2.SubItems.Add(num2.ToString());
				listView.Items.Add(listViewItem2);
			}
		}

		private void InitialLVColumn(ref ListView lv, string colNum)
		{
			lv.GridLines = true;
			lv.FullRowSelect = true;
			lv.View = View.Details;
			lv.Scrollable = true;
			lv.MultiSelect = false;
			lv.HeaderStyle = ColumnHeaderStyle.Nonclickable;
			lv.Columns.Add(colNum, 50, HorizontalAlignment.Left);
			lv.Columns.Add("V", 50, HorizontalAlignment.Left);
			lv.Visible = true;
		}

		private double CalcRecDuctSpeed(double flow, int width, int height)
		{
			return Convert.ToDouble(StringTools.CutOffEfficacious((flow / 3600.0 / ((double)width / 1000.0 * (double)height / 1000.0)).ToString(), 2));
		}

		private double CalcRouDuctSpeed(double flow, int diameter)
		{
			flow /= 3600.0;
			double num = (double)diameter / 1000.0;
			return Convert.ToDouble(StringTools.CutOffEfficacious((flow / (Math.PI * num * num / 4.0)).ToString(), 2));
		}

		private List<int> GetRectangeSec()
		{
			return new List<int>
			{
				120,
				160,
				200,
				250,
				320,
				400,
				500,
				630,
				800,
				1000,
				1250,
				1600,
				2000
			};
		}

		private List<int> GetRoundSec()
		{
			return new List<int>
			{
				80,
				90,
				100,
				110,
				120,
				130,
				140,
				150,
				160,
				170,
				180,
				190,
				200,
				210,
				220,
				240,
				250,
				260,
				280,
				300,
				320,
				340,
				360,
				380,
				400,
				420,
				450,
				480,
				500,
				530,
				560,
				600,
				630,
				670,
				700,
				750,
				800,
				850,
				900,
				950,
				1000,
				1060,
				1120,
				1180,
				1250,
				1320,
				1400,
				1500,
				1600,
				1700,
				1800,
				1900,
				2000
			};
		}

		private void CalcDuctForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.cds.DuctFlow = Convert.ToDouble(this.txtFlow.Text);
			this.cds.SecType = this.cmbSection.SelectedIndex;
			this.cds.DuctWidth = Convert.ToInt32(this.txtWidth.Text);
			if (this.cds.SecType == 0)
			{
				this.cds.DuctHeight = Convert.ToInt32(this.txtHeight.Text);
			}
			this.useSetting.WriteCalcDuctSpeed(this.cds);
		}

		private void cmbSection_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.ComBoxChangeBanding();
		}

		private void txtFlow_TextChanged(object sender, EventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			if (Convert.ToDouble(hytextBox.Text) < 1.0)
			{
				YJKMessageBox.Information("风管流量应该大于1。");
				return;
			}
			this.ComBoxChangeBanding();
		}

		private void txtFlow_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			if (Convert.ToDouble(hytextBox.Text) < 1.0)
			{
				YJKMessageBox.Information("风管流量应该大于1。");
				e.Cancel = true;
			}
		}

		private void CalcDuctForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void txtHeight_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			if (Convert.ToDouble(hytextBox.Text) < 1.0)
			{
				YJKMessageBox.Information("风管尺寸应该大于1的正整数。");
				e.Cancel = true;
			}
		}

		private void btnChange_Click(object sender, EventArgs e)
		{
			int num = Convert.ToInt32(this.txtWidth.Text);
			int num2 = Convert.ToInt32(this.txtHeight.Text);
			this.txtWidth.Text = num2.ToString();
			this.txtHeight.Text = num.ToString();
		}

		private void lvWidth_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lvWidth.SelectedItems.Count > 0)
			{
				string text = this.lvWidth.SelectedItems[0].SubItems[0].Text;
				this.txtWidth.Text = text;
				System.Drawing.Color backColor = this.lvWidth.BackColor;
				System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
				foreach (object obj in this.lvWidth.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					listViewItem.BackColor = backColor;
					if (listViewItem.Index == this.lvWidth.SelectedItems[0].Index)
					{
						listViewItem.BackColor = lightSkyBlue;
					}
				}
			}
		}

		private void lvHeight_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lvHeight.SelectedItems.Count > 0)
			{
				string text = this.lvHeight.SelectedItems[0].SubItems[0].Text;
				this.txtHeight.Text = text;
				System.Drawing.Color backColor = this.lvWidth.BackColor;
				System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
				foreach (object obj in this.lvHeight.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					listViewItem.BackColor = backColor;
					if (listViewItem.Index == this.lvHeight.SelectedItems[0].Index)
					{
						listViewItem.BackColor = lightSkyBlue;
					}
				}
			}
		}

		private void lblSpeed_TextChanged(object sender, EventArgs e)
		{
			string text = this.lblSpeed.Text;
			if (string.IsNullOrEmpty(text) || text.Equals("V(m/s) = "))
			{
				return;
			}
			string value = text.Substring(8);
			try
			{
				double num = Convert.ToDouble(value);
				if (num > 0.7 && num <= 1.0)
				{
					this.lblSpeed.ForeColor = System.Drawing.Color.Red;
				}
				else if (num > 1.0 && num <= 2.0)
				{
					this.lblSpeed.ForeColor = System.Drawing.Color.FromArgb(33, 40, 48);
				}
				else if (num > 2.0 && num <= 3.0)
				{
					this.lblSpeed.ForeColor = System.Drawing.Color.FromArgb(0, 127, 0);
				}
				else if (num > 3.0 && num <= 4.0)
				{
					this.lblSpeed.ForeColor = System.Drawing.Color.FromArgb(127, 95, 0);
				}
				else
				{
					this.lblSpeed.ForeColor = System.Drawing.Color.Black;
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void CalcDuctForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void CalcDuctForm_MouseLeave(object sender, EventArgs e)
		{
		}

		private HVACUserSetting useSetting;

		private CalcDuctSpeed cds;

		private ControlInfoManager m_thisManger;
	}
}
